package interview.algortithm;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: zhangrongqing
 * @Date: 2020/5/16 16:28
 */
public class TurpleSort {

    public List<Turple> sort(List<Turple> tuples){
        LinkedList<Turple> result = new LinkedList<>();
        for(Turple turple:tuples){
            if(result.size() == 0){
                result.add(turple);
                continue;
            }

            for(int i = 0;i <= result.size() - 1;i++){
                if(result.get(i).getStart() >= turple.getEnd()){
                    result.add(i,turple);
                    break;
                }
                if(result.size() - 1 == i){
                    result.add(turple);
                    break;
                }
            }
        }

        return result;

    }

    public static void main(String[] args) {
        List<Turple> tuples = new ArrayList<>();
        tuples.add(new Turple(1, 2));
        tuples.add(new Turple(4, 4));
        tuples.add(new Turple(3, 4));
        tuples.add(new Turple(4, 5));
        List<Turple> sort = new TurpleSort().sort(tuples);
        System.out.println(sort);
    }


}

@Data
@AllArgsConstructor
class Turple{
    private int start;
    private int end;
}
